'use strict';

layui.use(['form', 'element', 'layer', 'jquery', 'laytpl'], function () {
    var form = layui.form(),
        element = layui.element(),
        layer = layui.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl;

    $('.add-users').click(function () {
        var id = $(this).attr('data-id');
        var _this = $(this);

        layer.open({
            type: 1,
            id: 'LAYER_USER_CONTAINER',
            title: '添加授权人员',
            btn: ['确认', '取消'],
            area: ['60%', '80%'],
            content: $('#user-container').html(),
            success: function success() {
                element.init(); // 避免弹出层加载成功后面板收缩效果失效，当弹层被打开后重新初始化
            },
            yes: function yes() {
                var users = [];
                $.each($('#LAYER_USER_CONTAINER input:checkbox:checked'), function (index) {
                    users.push($(this).val());
                });
                var loading = layer.load(2, {
                    shade: [0.5, '#fff']
                });
                addAuthorizationUsers(id, users).then(function (response) {
                    if (response.errno) {
                        layer.msg(response.message, { icon: 2, time: 3000 });
                    } else {
                        layer.closeAll();
                        var html = '';
                        for (var item in response.data.Users) {
                            html += '<button class="btn btn-info btn-xs remove-user" data-id="' + response.data.id + '" data-category="' + response.data.category_id + '" data-user-id="' + response.data.Users[item].id + '">' + response.data.Users[item].nickname + ' \xD7</button>';
                        }
                        _this.prev().html(html);
                        layer.msg(response.message, { icon: 1, time: 1500 });
                    }
                });
            }
        });
    });

    $('.table').on('click', '.remove-user', function () {
        var id = $(this).attr('data-id'),
            user_id = $(this).attr('data-user-id'),
            _this = $(this);
        removeUserAuthorization(id, user_id).then(function (response) {
            if (response.errno) {
                layer.msg(response.message, { icon: 2, time: 3000 });
            } else {
                var html = '';
                for (var item in response.data.Users) {
                    html += '<button class="btn btn-info btn-xs remove-user" data-id="' + response.data.id + '" data-category="' + response.data.category_id + '" data-user-id="' + response.data.Users[item].id + '">' + response.data.Users[item].nickname + ' \xD7</button>';
                }
                _this.parent().html(html);
                layer.msg(response.message, { icon: 1, time: 1500 });
            }
        });
    });

    function addAuthorizationUsers(id, users) {
        return new Promise(function (resolve, reject) {
            $.post('/approval/addPermissionTo', {
                id: id,
                users: users
            }, function (response) {
                resolve(response);
            }, 'json');
        });
    }

    function removeUserAuthorization(id, user_id) {
        return new Promise(function (resolve, reject) {
            $.post('/approval/removePremission', {
                id: id,
                user_id: user_id
            }, function (response) {
                resolve(response);
            }, 'json');
        });
    }
});